/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-25 10:35:13
 * @LastEditors: TT
 * @LastEditTime: 2023-10-25 10:39:16
 */
import 'package:extended_image/extended_image.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_editor/image_editor.dart';

class ImageEditorTool {
  /// 编辑图片，并返回图片
  static Future<dynamic> cropImageDataWithBty({
    required ExtendedImageEditorState state,
    bool isByte = true,
  }) async {
    final Rect? cropRect = state.getCropRect();
    final EditActionDetails action = state.editAction!;
    final int rotateAngle = action.rotateAngle.toInt();
    final bool flipHorizontal = action.flipY;
    final bool flipVertical = action.flipX;
    final Uint8List img = state.rawImageData;
    final ImageEditorOption option = ImageEditorOption();
    if (action.needCrop) {
      option.addOption(
        ClipOption.fromRect(
          cropRect!,
        ),
      );
    }
    if (action.needFlip) {
      option.addOption(
        FlipOption(
          horizontal: flipHorizontal,
          vertical: flipVertical,
        ),
      );
    }
    if (action.hasRotateAngle) {
      option.addOption(
        RotateOption(
          rotateAngle,
        ),
      );
    }
    if (!kIsWeb) {
      if (isByte) {
        var result = await ImageEditor.editImage(
          image: img,
          imageEditorOption: option,
        );
        return result;
      } else {
        var result = await ImageEditor.editImageAndGetFile(
          image: img,
          imageEditorOption: option,
        );
        return result;
      }
    } else {
      return null;
    }
  }
}
